package fr.enseirbmatmeca.apms.tapestry.pages;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;

import fr.enseirbmatmeca.apms.spring.model.project.Project;
import fr.enseirbmatmeca.apms.spring.model.project.ProjectCollection;
import fr.enseirbmatmeca.apms.spring.model.project.ProjectGroup;
import fr.enseirbmatmeca.apms.spring.model.user.User;
import fr.enseirbmatmeca.apms.spring.service.UserManager;

/**
 * Start page of application apms.
 */
public class Index extends Security
{
    @Property
    @Inject
    @Symbol(SymbolConstants.TAPESTRY_VERSION)
    private String tapestryVersion;

    @InjectComponent
    private Zone zone;

    @Persist
    @Property
    private int clickCount;

    @Inject
    private AlertManager alertManager;
    
    @Property
	private User user;
	
	public Date getCurrentTime()
    {
        return new Date();
    }
	

    public int getUserNumber() {
		return userManager.findAll().size();
	}

    void onActionFromIncrement()
    {
        alertManager.info("Increment clicked");

        clickCount++;
    }
    
    void onActionFromAddRandomUser()
    {
    	User user = new User();
    	Random rd = new Random();
    	user.setLogin("test user "+rd.nextInt());
    	user.setPassword("test");
    	user.setTitle(User.TITLE_MAN);
    	user.setFirstName("test");
    	user.setLastName("test");
    	user.setType(User.USER);
        userManager.createUser(user);
        alertManager.info("Added user : "+user.getLogin());
    }

    Object onActionFromIncrementAjax()
    {
        clickCount++;

        alertManager.info("Increment (via Ajax) clicked");

        return zone;
    }
    
    void onActionFromSetupDB()
    {
    	Random rd = new Random();
    	
    	
    	Project p = new Project();
    	p.setDescription("projet de test "+rd.nextInt()+" pour vérifier le fonctionnement");
    	p.setName("projet de test "+rd.nextInt());
    	p.setTags("test;testComplexe");
    	projectManager.createProject(p);

    	ArrayList<User> tutors = new ArrayList<User>();
    	ArrayList<User> reporters = new ArrayList<User>();
    	ArrayList<User> students = new ArrayList<User>();

    	User user = new User();
    	user.setLogin("test tutor "+rd.nextInt());
    	user.setPassword("test");
    	user.setTitle(User.TITLE_MAN);
    	user.setFirstName("test");
    	user.setLastName("tutor");
    	user.setType(User.USER);
    	user.setPromotion("glrt");
        userManager.createUser(user);
        tutors.add(user);
        
        user = new User();
    	user.setLogin("test student "+rd.nextInt());
    	user.setPassword("teststudent");
    	user.setTitle(User.TITLE_MAN);
    	user.setFirstName("test");
    	user.setLastName("student");
    	user.setType(User.USER);
    	user.setPromotion("glrt");
    	userManager.createUser(user);
        students.add(user);
        
        user = new User();
    	user.setLogin("test reporter "+rd.nextInt());
    	user.setPassword("testreporter");
    	user.setTitle(User.TITLE_MAN);
    	user.setFirstName("test");
    	user.setLastName("reporter");
    	user.setType(User.USER);
    	user.setPromotion("glrt");
        userManager.createUser(user);
        reporters.add(user);
        
    	ProjectGroup pg = new ProjectGroup();
    	pg.setProject(p);

    	ProjectCollection pc = new ProjectCollection();
    	ArrayList<Project> listProject= new ArrayList<Project>();
    	listProject.add(p);
    	pc.setProjects(listProject);
    	pc.setName("Collection de projet test "+rd.nextInt());
    	pc.setDescription("Collection de projet test "+rd.nextInt()+" pour vérifier le fonctionnement");
    	pc.setDeadline(new Date(System.currentTimeMillis()+100000000L));
    	pc.setPromotion("glrt");
    	pc.setStatus(ProjectCollection.STATUS_CHOIX_PROJETS);
        
        pg.setReporters(reporters);
        pg.setStudents(students);
        pc.setTutors(tutors);
        
        projectManager.CreateProjectCollection(pc);
        projectManager.CreateProjectGroup(pg);
        
        
        alertManager.info("DB setup completed");
    }
}
